package com.lecloud.playerdemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lecloud.lecloudsdkdemo2.R;
import com.lecloud.playerdemo.MApplication;
import com.lecloud.sdk.constant.PlayerParams;

public class VodActivity extends Activity implements OnClickListener {

    String uuid 	= "aaotirawkw";
    String vuid 	= "b685574906";
//    String playPath = "http://yuntv.letv.com/bcloud.swf?uu=aaotirawkw&vu=b685574906&auto_play=1&gpcflag=1&lang=zh_CN";
    String playPath = "http://cache.utovr.com/201601131107187320.mp4";
    String mPU = "0";
    boolean hasSkin;
    private ImageView mBack;
    private TextView mTitle;
    private Button mVodId;
    private EditText mUUidContent;
    private EditText mVUidContent;
    private LinearLayout mUUid;
    private LinearLayout mVUid;
    private Button mVodPath;
    private EditText mVodPathContent;
    private EditText mPUContent;
    private LinearLayout mPlayPath;
    //	String mPlayPath = "";
    private CheckBox checkBoxPano;
    private Button mStartVod;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vod);
        hasSkin = getIntent().getBooleanExtra("hasSkin", false);
        initView();
        setOnClickListener();
        mVodId.setSelected(true);
        mVodPath.setSelected(false);
    }

    private void initView() {
        mVodId = (Button) findViewById(R.id.vodId);
        mVodPath = (Button) findViewById(R.id.vodPath);
        mStartVod = (Button) findViewById(R.id.startVod);

        mUUidContent = (EditText) findViewById(R.id.uuid_content);
        mUUidContent.setText(uuid);
        mVUidContent = (EditText) findViewById(R.id.vuid_content);
        mVUidContent.setText(vuid);

        mVodPathContent = (EditText) findViewById(R.id.vodPath_content);
        mVodPathContent.setText(playPath);

        mPUContent = (EditText) findViewById(R.id.vodPu_content);
        mPUContent.setText(mPU);

        mUUid = (LinearLayout) findViewById(R.id.vod_uuid);
        mVUid = (LinearLayout) findViewById(R.id.vod_vuid);
        mPlayPath = (LinearLayout) findViewById(R.id.vod_playPath);
        mBack = (ImageView) findViewById(R.id.back);
        mTitle = (TextView) findViewById(R.id.title);
        if (hasSkin) {
            mTitle.setText(getResources().getString(R.string.vod_hasSkin));
        } else {
            mTitle.setText(getResources().getString(R.string.vod_noSkin));
        }

        checkBoxPano = (CheckBox) findViewById(R.id.pano);
    }

    private void setOnClickListener() {
        mVodId.setOnClickListener(this);
        mVodPath.setOnClickListener(this);
        mStartVod.setOnClickListener(this);
        mUUid.setOnClickListener(this);
        mVUid.setOnClickListener(this);
        mPlayPath.setOnClickListener(this);
        mBack.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.vodId:
                mVodId.setSelected(true);
                mVodPath.setSelected(false);
                mPlayPath.setVisibility(View.GONE);
                mUUid.setVisibility(View.VISIBLE);
                mVUid.setVisibility(View.VISIBLE);
                mPUContent.setVisibility(View.VISIBLE);
                break;
            case R.id.vodPath:
                mVodId.setSelected(false);
                mVodPath.setSelected(true);
                mPlayPath.setVisibility(View.VISIBLE);
                mUUid.setVisibility(View.GONE);
                mVUid.setVisibility(View.GONE);
                mPUContent.setVisibility(View.GONE);
                break;
            case R.id.startVod:
                // 开始点播
                if (mVodPath.isSelected() && TextUtils.isEmpty(mVodPathContent.getText().toString().trim())) {
                    Toast.makeText(VodActivity.this, getResources().getString(R.string.no_play_path), Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!MApplication.cdeInitSuccess){
                    Toast.makeText(VodActivity.this, "CDE未初始化完成,不能播放...", Toast.LENGTH_SHORT).show();
                    return;
                }
                startLecloudVod();
                break;
            case R.id.back:
                VodActivity.this.finish();
                break;
            default:
                break;
        }
    }

    /**
     * 乐视云点播
     */
    private void startLecloudVod() {
        Intent intent = new Intent(VodActivity.this, PlayActivity.class);
        Bundle mBundle = new Bundle();
        mBundle.putInt(PlayerParams.KEY_PLAY_MODE, PlayerParams.VALUE_PLAYER_VOD);
        if (mVodId.isSelected()) {
            mBundle.putString(PlayerParams.KEY_PLAY_UUID, mUUidContent.getText().toString().trim());
            mBundle.putString(PlayerParams.KEY_PLAY_VUID, mVUidContent.getText().toString().trim());
            mBundle.putString(PlayerParams.KEY_PLAY_CHECK_CODE, "");
            mBundle.putString(PlayerParams.KEY_PLAY_PAYNAME, "0");
            mBundle.putString(PlayerParams.KEY_PLAY_USERKEY, "151398");
//			mBundle.putString(PlayerParams.KEY_PLAY_BUSINESSLINE, "101");
            mBundle.putString(PlayerParams.KEY_PLAY_PU, mPUContent.getText().toString().trim());
            mBundle.putString(PlayerParams.KEY_CUID, "123");
            mBundle.putString(PlayerParams.KEY_UTOKEN, "1234");
        } else {
            mBundle.putString("path", mVodPathContent.getText().toString().trim());
        }
        mBundle.putBoolean("pano", checkBoxPano.isChecked());
        mBundle.putBoolean("hasSkin", hasSkin);
        intent.putExtra(PlayActivity.DATA, mBundle);
        startActivity(intent);
    }


}
